<template>
    <view>
        <!-- pages/user/integral/index.wxml -->
        <!-- 头部 -->
        <navber id="head-navber" :navbarData="navbarData"></navber>

        <!-- 积分 -->
        <view class="integral">
            <view class="integral-info">
                <image :src="imgUrl + '/imgs/user/integral/bac.png'" class="integral-info-img"></image>
                <view class="integral-info-title">- 当前积分 -</view>
                <view class="integral-info-number">{{ userIntegral }}</view>
            </view>
            <view class="integral-but">
                <view class="integral-but-jl">
                    <view class="integral-but-jl-text" @tap="switch_check($event, { value: 'jl' })" data-value="jl">
                        积分记录
                        <view class="integral-but-jl-text-bottom" v-if="butCheck == 'jl'"></view>
                    </view>
                </view>
                <view class="integral-but-gz">
                    <view class="integral-but-gz-text" @tap="switch_check($event, { value: 'gz' })" data-value="gz">
                        积分规则
                        <view class="integral-but-gz-text-bottom" v-if="butCheck == 'gz'"></view>
                    </view>
                </view>
            </view>
        </view>

        <!-- 积分记录 -->
        <view class="integral-list" v-if="butCheck == 'jl'">
            <view class="integral-list-item" v-for="(item, index) in dataList" :key="index">
                <view class="integral-list-item-text1">{{ item.add_date }}</view>

                <view class="integral-list-item-text2">{{ item.detail }}</view>

                <view class="integral-list-item-line"></view>

                <view class="integral-list-item-text3">{{ item.symbol }}{{ item.number }}</view>
            </view>
        </view>

        <!-- 积分规则 -->

        <view class="integral-gz" v-if="butCheck == 'gz'">
            <view>
                {{ ruleContent }}
            </view>
        </view>
    </view>
</template>

<script>
import navber from '@/components/navber/index';
// pages/user/integral/index.js
const Config = require('../../../config/index.js');

const Api = require('../../../api/index.js');

export default {
    components: {
        navber
    },
    data() {
        return {
            navbarData: {
                title: '我的积分' //顶部标题
            },
            //导航栏
            imgUrl: Config.imgUrl,
            //图片url
            butCheck: 'jl',
            dataList: [],
            pagenow: 1,
            userIntegral: 0,
            ruleContent: ''
        };
    },
    onReachBottom: function (e) {
        //滚动底部
        var page = this.pagenow + 1;
        this.setData({
            pagenow: page
        });
        this.getData();
    },
    onShow() {
        this.setData({
            dataList: []
        });
        this.getData();
        var json = {
            type: 1
        };
        Api.getRule(json).then((res) => {
            this.setData({
                ruleContent: res.data.rule
            });
        });
    },
    onPullDownRefresh(e) {
        this.setData({
            dataList: [],
            pagenow: 1
        });
        this.getData();
        uni.stopPullDownRefresh();
    },
    methods: {
        switch_check(e, _dataset) {
            
            if (!e.currentTarget) {
                if (_dataSet.tagId) {
                    e.currentTarget = {
                        id: _dataSet.tagId
                    };
                } else {
                    e.currentTarget = {
                        dataset: _dataSet
                    };
                }
            }
            
            this.setData({
                butCheck: e.currentTarget.dataset.value
            });
        },

        getData() {
            //获取数据
            var json = {
                pagenow: this.pagenow
            };
            Api.userIntegral(json).then((res) => {
                if (res.data.list.length > 0) {
                    var data = this.dataList;
                    data = data.concat(res.data.list);
                    this.setData({
                        userIntegral: res.data.integral,
                        dataList: data
                    });
                } else {
                    uni.showToast({
                        title: '暂无数据',
                        icon: 'none',
                        duration: 2000
                    });
                } // wx.stopPullDownRefresh();
            });
        }
    }
};
</script>
<style>
@import './index.css';
</style>
